package com.qc31.monitorlib.ui.video.observer

import com.qc31.baselib.utils.MLog
import java.util.*

class NeedMutesUtil {
    private val observerHashMap = Collections.synchronizedMap(HashMap<String, NeedMutesObserver>())
    private var playKey: String = ""

    fun attachObserver(key: String, observer: NeedMutesObserver) {
        MLog.e("绑定=========== $key")
        observerHashMap[key] = observer
    }

    fun detachObserver(key: String) {
        observerHashMap.remove(key)
    }

    fun  setAudioState(key: String, isNeedMute: Boolean) {
        if (playKey.isNotEmpty() && key == playKey) {
            observerHashMap[playKey]?.changeNeedMutes(isNeedMute)
        } else {
            observerHashMap.forEach { (keyed, observer) ->
                MLog.e("绑定========forEach=== $keyed")
                if (key == keyed) observer.changeNeedMutes(isNeedMute)
                else observer.changeNeedMutes(true)
            }
        }
        playKey = key

    }

    fun clearAll() {
        observerHashMap.clear()
    }

    companion object {
        val instance: NeedMutesUtil by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            NeedMutesUtil()
        }
    }
}